package components.criteria;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;

import javax.swing.Timer;

import menus.demm.DEMM;
import menus.flower.FlowerMenu;
import menus.zone.ZoneMenu;

import components.SetupExperiment;
import components.factors.Technique.TechniqueInterface;
import components.intertitles.BlockInstructions;
import components.intertitles.Trial;

import fr.inria.insitu.noe.Platform;
import fr.inria.insitu.noe.endConditions.AbstractEndCondition;
import fr.lri.swingstates.canvas.CShape;
import fr.lri.swingstates.canvas.Canvas;

public class ClickOnButton extends AbstractEndCondition {
	
	protected CShape pickedShape;
	protected Timer platformTimer;
	
	protected Canvas canvas;
	protected CShape startButton;
	
	public ClickOnButton() {
	
	}
	
	@Override
	public boolean isReached(Timer timer, long when) {
        // return false;
		
		platformTimer = (Timer)Platform.getInstance().getMeasureValue(BlockInstructions.InstructionsTimer);
		
		if (timer.equals(platformTimer)){
			return true;
		}
		
		return false;
		
    }
	

	@Override
    public boolean isReached(InputEvent e) {
		
		if (e.getID() == MouseEvent.MOUSE_PRESSED && canvas.pick(((MouseEvent)e).getPoint()) == startButton) {
			
			canvas.removeShapes(canvas.getTag(BlockInstructions.InstructionTag));
			canvas.removeShapes(canvas.getTag(BlockInstructions.PauseTag));
			
			Timer timer = new Timer(2000, new ActionListener(){
				
				public void actionPerformed(ActionEvent e) {
				}
				
			});
			
			timer.setRepeats(false);
			
			Platform.getInstance().addTimer(timer);
			
			timer.start();
			
			Platform.getInstance().setMeasureValue(BlockInstructions.InstructionsTimer, timer);
			
			drawMenu();
			
		}
		
		return false;
        
    }
	
	public void start() {
		
		canvas = ((SetupExperiment)Platform.getExperiment().getSetUp()).getGestureArea();
		startButton = (CShape)Platform.getInstance().getFactorValue(SetupExperiment.START_BUTTON);
		
		super.start();
		pickedShape = null;
		Platform.getInstance().registerComponent(canvas);
		
	}

	public void stop() {
		super.stop();
	}
	
	protected void drawMenu(){
		String tech = ((TechniqueInterface)Platform.getInstance().getFactorValue(Trial.Technique)).toString();
		if (tech.contains("DEMM"))
			DEMM.drawMenu();
		else if (tech.contains("Flower"))
			FlowerMenu.drawMenu();
		else if (tech.contains("Zone"))
			ZoneMenu.drawMenu();
	}
	
}
